<template>
  <view class="container">
    <view class="input">
      <input v-model="username" placeholder="请输入用户名" />
    </view>
    <view class="input">
      <input v-model="password" type="password" placeholder="请输入密码" />
    </view>
    <view class="input">
      <input v-model="phone" placeholder="请输入手机号（可选）" />
    </view>
    <view class="btn-primary" @click="handleRegister">注册</view>
    <view style="margin-top:16px;text-align:center;">
      <text @click="goLogin" style="color:#07c160;">已有账号？去登录</text>
    </view>
  </view>
</template>

<script>
import { register } from '../../api/user.js'
export default {
  data() {
    return {
      username: '',
      password: '',
      phone: ''
    }
  },
  methods: {
    handleRegister() {
      if (!this.username || !this.password) {
        uni.showToast({ title: '请输入用户名和密码', icon: 'none' })
        return
      }
      register({ username: this.username, password: this.password, phone: this.phone }).then(result => {
        // 现在返回的是完整的result对象
        if (result.code === 200) {
          uni.showToast({ title: '注册成功', icon: 'success' })
          setTimeout(() => {
            uni.redirectTo({ url: '/pages/login/login' })
          }, 800)
        } else {
          uni.showToast({ title: result.message || '注册失败', icon: 'none' })
        }
      }).catch(err => {
        // 错误已经在 request.js 中处理了
        console.error('注册失败:', err)
      })
    },
    goLogin() {
      uni.redirectTo({ url: '/pages/login/login' })
    }
  }
}
</script>

<style scoped>
.container {
  padding: 40px 24px;
}
</style> 